package com.lg.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    public static final String EX_PAY_DLX = "ex.pay.dlx";
    public static final String QUEUE_PAY_DLX = "queue.pay.dlx";
    public static final String EX_PAY_NORMAL = "ex.pay.normal";
    public static final String QUEUE_PAY_NORMAL = "queue.pay.normal";
    public static final String SINGLE_LISTENER_FACTORY = "singleListenerContainer";

    private static final String X_DEAD_LETTER_EXCHANGE = "x-dead-letter-exchange";
    private static final String X_DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";
    private static final String X_MESSAGE_TTL = "x-message-ttl";
    private static final Integer TTL = 10000;

    @Autowired
    private CachingConnectionFactory connectionFactory;

    /**
     * 设置单一消费者
     */
    @Bean(name = SINGLE_LISTENER_FACTORY)
    public SimpleRabbitListenerContainerFactory listenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setConcurrentConsumers(1);
        factory.setMaxConcurrentConsumers(1);
        factory.setPrefetchCount(1);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }


    /**
     * 定义支付的死信交换器
     */
    @Bean
    public DirectExchange exPayDlx() {
        return new DirectExchange(EX_PAY_DLX, false, false, null);
    }

    /**
     * 定义支付的死信队列
     */
    @Bean
    public Queue queuePayDlx() {
        return new Queue(QUEUE_PAY_DLX, false, false, false, null);
    }

    /**
     * 将支付的死信队列绑定到支付的死信交换器
     */
    @Bean
    public Binding bindDlxExAndQueue(@Qualifier("queuePayDlx") Queue queue, @Qualifier("exPayDlx") DirectExchange ex) {
        return BindingBuilder.bind(queue).to(ex).withQueueName();
    }

    /**
     * 定义正常支付的交换器
     */
    @Bean
    public DirectExchange exPayNormal() {
        return new DirectExchange(EX_PAY_NORMAL, false, false, null);
    }

    /**
     * 定义正常支付的队列，并绑定死信交换器和死信队列
     */
    @Bean
    public Queue queuePayNormal() {
        Map<String, Object> args = new HashMap<>(3);
        args.put(X_DEAD_LETTER_EXCHANGE, EX_PAY_DLX);
        args.put(X_DEAD_LETTER_ROUTING_KEY, QUEUE_PAY_DLX);
        args.put(X_MESSAGE_TTL, TTL);
        return new Queue(QUEUE_PAY_NORMAL, false, false, false, args);
    }

    /**
     * 将支付的正常队列绑定到支付的正常交换器
     */
    @Bean
    public Binding bindNormalExAndQueue(@Qualifier("queuePayNormal") Queue queue, @Qualifier("exPayNormal") DirectExchange ex) {
        return BindingBuilder.bind(queue).to(ex).withQueueName();
    }

}
